package com.sogoodsoft.service.crm;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.sogoodsoft.dao.common.CommonHibernateDao;
import com.sogoodsoft.dao.crm.AlertDao;
import com.sogoodsoft.entity.account.User;
import com.sogoodsoft.entity.app.SysClass;
import com.sogoodsoft.entity.crm.Alert;
import com.sogoodsoft.entity.crm.Smstask;
import com.sogoodsoft.service.account.AccountManager;
import com.sogoodsoft.service.app.SysClassService;
import com.sogoodsoft.service.common.AbstractService;
import com.sogoodsoft.util.InfosysSecurityUtil;

@Component
@Transactional
public class AlertService extends AbstractService<Alert, Long> {
	
	@Autowired
	private AlertDao dao;
	
	@Autowired
	private SmstaskService smstaskService;
	
	@Autowired
	private SysClassService sysService;
	
	@Autowired
	private AccountManager accountManager;
	
	@Override
	public CommonHibernateDao<Alert, Long> getDao() {
		return dao;
	}
	@Override
	public void save(Alert t) {		
		
		//删除已有短信提醒
		if(t.getId()!=null){
			smstaskService.delete(t.getSmsid());
			t.setSmsid(null);			
		}
		
		dao.save(t);
		User user = accountManager.getUser(t.getUserid());
		if (t.getSmsalert()!=null&&t.getSmsalert().longValue() == 1) {
			if (t.getSelf()!=null&&t.getSelf().longValue() == 1) {
				//个人提醒
				Smstask task = new Smstask();
				
				task.setTarget(user.getMobile());
				task.setMsg(t.getSummary());
				task.setPlantime(t.getAlerttime());
				//添加taskUSERid
				task.setSourceid(t.getId());
				SysClass type = sysService
						.getByCode(Smstask.SEND_TYPE_CODE.ALERT.getCODE());
				task.setType(type);
				SysClass status = sysService
						.getByCode(Smstask.SEND_STATUS_CODE.WAIT.getCODE());
				task.setStatus(status);
				smstaskService.save(task);
				t.setSmsid(task.getId());
			}else{
				//公司提醒
				Smstask task = new Smstask();
				
				//所有人的手机号
				String target = accountManager.getAllUserMobile();
				task.setTarget(target);
				task.setMsg(t.getSummary());
				task.setPlantime(t.getAlerttime());
				//添加taskUSERid
				task.setUserid(user.getId());
				task.setUsername(user.getName());
				task.setSourceid(t.getId());
				SysClass type = sysService
						.getByCode(Smstask.SEND_TYPE_CODE.ALERT.getCODE());
				task.setType(type);
				SysClass status = sysService
						.getByCode(Smstask.SEND_STATUS_CODE.WAIT.getCODE());
				task.setStatus(status);
				smstaskService.save(task);
				t.setSmsid(task.getId());
			}
			dao.save(t);
		}
	}
	

	/**
	 * 提醒完成，更新状态
	 * @param id
	 */
	public void AlertPublished(Long id){
		Alert alert = dao.get(id);
		alert.setPublished(1L);
		dao.save(alert);
	}
	
	/**
	 * 如果有短信或者提醒已发出，或提醒完成，则禁止修改
	 * @param t
	 * @return
	 */
	public boolean editable(Alert t){
		boolean editable = true;
		
		//本提醒判断

		if(t.getPublished()!=null&&t.getPublished().longValue()==1){
			return false;
		}
		
		//短信判断
		String hql = "from Smstask where sourceid=?";
		Smstask task = smstaskService.getDao().findUnique(hql,t.getId());
		if(task!=null){
			return smstaskService.editable(task);
		}
		
		return editable;
	}
	
	/**
	 * 用户的登录提示
	 * @return
	 */
	public List<Alert> getCurrUserLoginAlert(){
		User user = InfosysSecurityUtil.getCurrentUser();
		List<Alert> list = new ArrayList<Alert>();
		//个人提醒
		List<Alert> self = dao.find("from Alert where  (published is null or published=2 ) and loginalert=1 and self=1 and userid=? and alerttime<=? ",user.getId(), new Date());
		if(self!=null&&self.size()>0){			
			list.addAll(self);
		}
		//公司提醒
		List<Alert> company = dao.find("from Alert where  (published is null or published=2 ) and loginalert=1 and  self=2 and alerttime<=?",new Date());
		if(company!=null&&company.size()>0){
			list.addAll(company);
		}
		
		return list;
	}
	
	/**
	 * 本次登录24小时内用户的定时提醒，假定用户每次的登录时间不会超过24小时间
	 * @return
	 */
	public List<Alert> getCurrUserSysAlert(){
		User user = InfosysSecurityUtil.getCurrentUser();
		if(user==null){return new ArrayList<Alert>();}
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 1);
		Date tomorrow = cal.getTime();
		List<Alert> list = new ArrayList<Alert>();
		//个人提醒
		List<Alert> self = dao.find("from Alert where  (published is null or published=2 ) and sysalert=1 and self=1 and userid=? and alerttime<=? ",user.getId(), tomorrow);
		if(self!=null&&self.size()>0){			
			list.addAll(self);
		}
		//公司提醒
		List<Alert> company = dao.find("from Alert where  (published is null or published=2 ) and sysalert=1 and  self=2 and alerttime<=?",tomorrow);
		if(company!=null&&company.size()>0){
			list.addAll(company);
		}
		
		return list;
	}
	
}
